# -*- coding: utf-8 -*-
# pywot - what's on tv today?
# Copyright (C) 2010 Ba1T <byte-f@live.it>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# -------------------------------------------------------------------------------

from urllib import *
from util import *
from threading import Thread

class WebThrd (Thread):
    def __init__(self, value):
        Thread.__init__(self)
        self.core = value

    def run (self):
		today = now ()
		progs = []
		num = 0
		print "Recupero la pagina per: Rai 1"
		self.core.sts ('Recupero la pagina per: Rai 1...')
		raiunop = getp ('http://www.miniportale.it/miniportale/tvoggi/programmi.asp?palinsesto=Rai Uno&data=%s' % (today))
		print "Recupero la pagina per: Rai 2"
		self.core.sts ('Recupero la pagina per: Rai 2...')
		raiduep = getp ('http://www.miniportale.it/miniportale/tvoggi/programmi.asp?palinsesto=Rai Due&data=%s' % (today))
		print "Recupero la pagina per: Rai 3"
		self.core.sts ('Recupero la pagina per: Rai 3...')
		raitrep = getp ('http://www.miniportale.it/miniportale/tvoggi/programmi.asp?palinsesto=Rai Tre&data=%s' % (today))
		print "Recupero la pagina per: Rete 4"
		self.core.sts ('Recupero la pagina per: Rete 4...')
		rete4p = getp ('http://www.miniportale.it/miniportale/tvoggi/programmi.asp?palinsesto=rete 4&data=%s' % (today))
		print "Recupero la pagina per: Canale 5"
		self.core.sts ('Recupero la pagina per: Canale 5...')
		canale5p = getp ('http://www.miniportale.it/miniportale/tvoggi/programmi.asp?palinsesto=Canale 5&data=%s' % (today))
		print "Recupero la pagina per: Italia 1"
		self.core.sts ('Recupero la pagina per: Italia 1...')
		italia1p = getp ('http://www.miniportale.it/miniportale/tvoggi/programmi.asp?palinsesto=Italia 1&data=%s' % (today))
		self.core.sts ('Parsing pages...')
		progsRaiUno = getdata (raiunop, 'Rai 1')
		num = len (progsRaiUno)
		progsRaiDue = getdata (raiduep, 'Rai 2')
		num += len (progsRaiDue)
		progsRaiTre = getdata (raitrep, 'Rai 3')
		num += len (progsRaiTre)
		progsRete4 = getdata (rete4p, 'Rete 4')
		num += len (progsRete4)
		progsCanale5 = getdata (canale5p, 'Canale 5')
		num += len (progsCanale5)
		progsItalia1 = getdata (italia1p, 'Italia 1')
		num += len (progsItalia1)
		progs = [progsRaiUno, progsRaiDue, progsRaiTre,
					progsRete4, progsCanale5, progsItalia1]
		progs.append (self.core.core.progz)
		#if s != None:
		self.core.core.progs = progs
		self.core.core.num = num
		self.core.sts ('Caricati %d programmi.' % (num))
		self.core.update_treeview ()
		
def getp (page):
	h = urlopen (page)
	r = h.read ()
	h.close ()
	return r
	
def getdata (s, ch):
	tmp = []
	trs = s.split ('<tr onMouseOver="this.bgColor')[1].split ('</tr>')[0]
	name = trs.split ('<td  width="360"')[1].split ('</td>')[0].split ('&nbsp;')[2]
	type = trs.split ('<td  width="130"')[1].split ('</td>')[0].split ('&nbsp;')[2].replace ('</b>', '')
	when = trs.split ('<td  width="20"')[1].split ('</td>')[0].split ('&nbsp;')[2]
	last_time = when
	err = False
	_id = 0
	tmp.append ((_id, name, when, type, ch))
	while err != True:
		try:
			h = s.split (last_time)[1]
			trs = h.split ('<tr onMouseOver="this.bgColor')[1].split ('</tr>')[0]
		except:
			err = True
		_id += 1
		name = trs.split ('<td  width="360"')[1].split ('</td>')[0].split ('&nbsp;')[2]
		type = trs.split ('<td  width="130"')[1].split ('</td>')[0].split ('&nbsp;')[2].replace ('</b>', '')
		when = trs.split ('<td  width="20"')[1].split ('</td>')[0].split ('&nbsp;')[2]
		last_time = when
		tmp.append ((_id, name, when, type, ch))
	return tmp
